package com.ctlele.team.space.activity.impl;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.ctlele.team.space.R;
import com.ctlele.team.space.util.InputStreamUtils;
import com.ctlele.team.space.widget.view.PageHeader;
import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.Markwon;
import io.noties.markwon.MarkwonConfiguration;
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
import io.noties.markwon.ext.tables.TablePlugin;
import io.noties.markwon.ext.tasklist.TaskListPlugin;
import io.noties.markwon.html.HtmlPlugin;
import io.noties.markwon.image.glide.GlideImagesPlugin;
import io.noties.markwon.syntax.SyntaxHighlightNoOp;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MarkdownPreviewActivity extends AppCompatActivity {

    private static final String TAG = "IjkPLayerActivity";

    public static final String URL_KEY = "url";

    public static final String TITLE_KEY = "title";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_markdown_preview);

        PageHeader header = findViewById(R.id.page_header);
        TextView markdownPreview = findViewById(R.id.markdown_preview);

        String title = getIntent().getStringExtra(TITLE_KEY);
        String url = getIntent().getStringExtra(URL_KEY);
        if (TextUtils.isEmpty(url) || !url.startsWith("http")) onBackPressed();

        TextView titleView = header.findViewById(R.id.title);
        if (!TextUtils.isEmpty(title)) {
            titleView.setText(title);
        }
        ImageView back = header.findViewById(R.id.go_back);
        back.setOnClickListener((view) -> onBackPressed());


        Markwon markwon = Markwon.builder(this)
                .usePlugin(GlideImagesPlugin.create(this))
                .usePlugin(new AbstractMarkwonPlugin() {
                    @Override
                    public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
                        builder.syntaxHighlight(new SyntaxHighlightNoOp());
                    }
                })
                .usePlugin(HtmlPlugin.create())
                .usePlugin(TablePlugin.create(this))
                .usePlugin(StrikethroughPlugin.create())
                .usePlugin(TaskListPlugin.create(this))
                .build();

        new Thread(() -> {
            try (InputStream inputStream = openStream(url)) {
                byte[] bytes = InputStreamUtils.readStream(inputStream);
                markwon.setMarkdown(markdownPreview, new String(bytes));
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
            }
        }).start();
    }

    InputStream openStream(String url) throws IOException {
        URL parse = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) parse.openConnection();
        connection.setRequestMethod("GET");
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            return connection.getInputStream();
        } else {
            return null;
        }
    }
}